// import store from'../store/index.js'
// import Base64 from 'base-64';
import app from '../App.vue';
import Vue from 'vue'
import util from './util.js';

import moment from './moment.js'



// #ifdef MP-WEIXIN
// const PID = 'WX209900100801';
const PID = util.PID;
// #endif


// const fileUploadPath = "https://intelligentdevice.youonbike.com/gaea/upload/publicRead";//文件上传地址正式
// const baseUrl = 'https://grouplock.youonbike.com/grouplock/share'; // 正式
// const hotelBaseUrl = 'https://alocktest.youonbike.com/kexinglock/share'; // 酒店门锁后台接口  正式
const baseUrl = 'https://alocktest.youonbike.com/kexinglock/share'; // 公寓 测试
const hotelBaseUrl = 'https://alocktest.youonbike.com/kexinglock/share'; // 酒店门锁后台接口  测试

const fileUploadPath = "https://sharelifetest.youonbike.com/gaea/upload/publicRead"; //文件上传地址测试

export default class Request {
	constructor(type = 'gongyu') {
		this.type = type;
	}
	http(options) {
		const {
			url,
			method,
			data
		} = options;
		let header = {
			// "Access-Control-Allow-Headers": "Content-Type, x-requested-with, X-Custom-Header, Request-Ajax",
			// "Access-Control-Allow-Methods": "*",
			// "Access-Control-Allow-Origin": "*",
			// "Access-Control-Allow-Credentials": "true"
		}

		// console.log('options',options);
		// 请求参数
		let base64data = util.Base64.encode(JSON.stringify(data || {})); //编码
		let reqdata = JSON.stringify(data || {});
		let opentime = moment().format('YYYY-MM-DD HH:MM:SS');
		let params = {
			'reqdata': base64data,
			'pid': PID,
			'opentime': opentime,
			'openId': Vue.prototype.$userInfo.openId,
			'version': '1.0'
		};
		let token = '';
		let source = opentime + PID + base64data + token;
		let sign = util.getSign(source);
		params.sign = sign;
		let rqUrl = '';
		if (this && this.type == 'gongyu') {
			rqUrl = baseUrl + options.url
		} else {
			rqUrl = hotelBaseUrl + options.url
		}
		console.log('request url->', rqUrl);
		console.log('入参->', JSON.stringify(data || {}));
		let result = new Promise((resolve, reject) => {
			uni.request({
				url: rqUrl,
				method: options.method,
				header: header,
				data: params,
				success: res => {
					console.log('接口响应->', JSON.stringify(res.data || {}))
					resolve(res.data)
				},
				fail: err => {
					reject(err)
				},
				complete: () => {
					// uni.hideToast()
				}
			})
		})
		if (result.statecode == "920009") { //token过期
			uni.navigateTo({
				url: '../../pagesA/login/codelogin'
			})
		}
		return result;
	};

	fileUpload(options) {
		// console.log('入参=====', options)
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: fileUploadPath,
				filePath: options.data,
				name: 'file',
				success: (uploadFileRes) => {
					resolve(uploadFileRes)
				},
				fail: err => {
					reject(err)
				},
				complete: () => {
					uni.hideToast()
				}
			});
		})
	}
}